<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreExpertRequest;
use App\Models\Expert;
use App\Models\Video;
use Illuminate\Http\Request;

class ExpertController extends Controller
{


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index()
    {
        $expert = Expert::paginate(9);
        return view('front.expert-front')
            ->with('expert',$expert);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function adminindex(){
        $expert = Expert::paginate(8);

        return view('admin.expert-show-list')->with('expert',$expert);
    }

    public function create()
    {
        return view('admin.expert-add-list');

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreExpertRequest $request)
    {
        $request->validated();//验证错误信息

        $data = $request->all();
        if($request->hasFile('photo')){
            $file = $request->file('photo');//获取文件
            $path = $file->store('expert_img','uploads');
            $filename='/uploads/'.$path;//构建文件地址路径
            $data["photo"]=$filename;
        }
        Expert::create($data);// 使用 create 方法创建新记录
        return view('admin.expertaddsuccess');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $expert = Expert::find($id);
        return view('front.expert-front-details')->with('expert',$expert);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $expert= Expert::find($id);
        $video=Video::find($id);
        redirect('/expert-show-list');
        return view('admin.expert-edit-list')->with('expert',$expert)->with('video',$video);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(StoreExpertRequest $request, $id)
    {
        $request->validated();//验证错误信息

        $data = $request->all();

        if($request->hasFile('photo')){
            $file = $request->file('photo');//获取文件
            $path = $file->store('expert_img','uploads');
            $filename='/uploads/'.$path;//构建文件地址路径
            $data["photo"]=$filename;
        }
        $expert=Expert::find($id);
        $expert->update($data);
        return view('admin.experteditsuccess');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $expert = Expert::find($id);
        $expert->delete();
        return view('admin.expertdeletesuccess');
    }
}
